package com.jxm.validator;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
import com.jxm.model.User;
import com.jxm.vo.IResult;

public class UserValidator extends Validator{
	
	protected void validate(Controller c) {
		this.setShortCircuit(true);//后面不会覆盖前面的校验
       validateRequiredString("username", "msg", "请输入用户名哦！");
       validateRegex("username", "^[a-zA-Z][a-zA-Z0-9]{3,15}$", "msg", "用户名格式不正确");
       if(User.dao.usernameIsExist(controller.getPara("username"))){
    	   addError("msg", "用户名已经存在");
       }
	}
    protected void handleError(Controller c) {
       c.renderJson(IResult.failed(c.getAttrForStr("msg")));
    }
}
